#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

#include <boost/shared_ptr.hpp>
#include <boost/tokenizer.hpp>

#include "Student.h"

StudentsPtr students(new Students);
CoursesPtr courses(new Courses);



bool
init(const std::string& filename)
{
  std::ifstream file_input(filename.c_str());
  if (!file_input.is_open())
    {
      std::cerr << "File " << filename << " not found!" << std::endl;
      return false;
    }


  std::string line;
  boost::char_separator<char> sep(" [-]");
  while (!file_input.eof())
    {
      std::getline(file_input, line);
      if (line != "")
	{
	  //std::cerr << "Processing line: " << line << std::endl;
	  
	  boost::tokenizer<boost::char_separator<char> > input(line, sep);
	  
	  boost::tokenizer<boost::char_separator<char> >::const_iterator beg = input.begin();
	  boost::tokenizer<boost::char_separator<char> >::const_iterator end = input.end();
	  boost::tokenizer<boost::char_separator<char> >::const_iterator it = beg;
	  ++it;
	  
	  if (*beg == "S")
	    {
              //std::cerr << "Got a student!" << *it << std::endl;
	      StudentPtr s(new Student(*it));
	      students->push_back(s);
	      ++it;
	      for(; it != end; ++it)
		{
		  std::back_insert_iterator<Courses> end_of_courses(*courses);
		  s->add_course(courses->begin(), courses->end(), end_of_courses, *it);
		}
	    }
	  else
	    {
	      //std::cerr << "Got a course! " << *it << std::endl;
	      Courses::const_iterator jt = std::find_if(courses->begin(), courses->end(), CompareCourses(*it));
	      
	      if (jt == courses->end())
		{
		  CoursePtr new_course(new Course(*it));
		  courses->push_back(new_course);
		  jt = courses->end();
		  --jt;
		}
	      
	      ++it;
	      for (; it != end; ++it)
	        {
	          //std::cerr << "Got time slot " << *it << std::endl; 
	          PtimePtr pt_start(new Ptime(boost::posix_time::from_iso_string(*it)));
		  ++it;
      	          PtimePtr pt_end(new Ptime(boost::posix_time::from_iso_string(*it)));
		  TimeSlotPtr ts(new TimeSlot(pt_start, pt_end));
	          (*jt)->add_time_slot(ts);		  
	        }
	    }
	}
    }
  file_input.close();
  
  std::cerr << students << std::endl;

  return true;
}


void
process()
{
  Students::const_iterator it = students->begin();
  Students::const_iterator end = students->end();

  for (; it != end; ++it)
    {
      const CoursesPtr& student_courses = (*it)->getCourses();
      CoursesPtr marked_courses(new Courses);

      Courses::const_iterator jt = student_courses->begin();
      Courses::const_iterator courses_end = student_courses->end();

      Courses::const_iterator conflict_course;
      Courses::const_iterator next_course;
      
      for (; jt != courses_end; ++jt)
	{
	  next_course = jt;
	  ++next_course;
	  conflict_course = std::find_if(next_course, courses_end, OverlappingCourses(*jt));

	  if (conflict_course != courses_end)
	    {
	      std::cout << "For " << (*it)->getName() << ": ";
	      std::cout << (*jt)->getCourseName() << " conflicts with " << (*conflict_course)->getCourseName() << std::endl;
	    }
	}
    }
}



int main(int argc, char* argv[])
{
  std::string filename;
  if (argc == 1)
    {
      filename = "student_plan.txt";
    }
  else
    {
      filename = argv[1];
    }

  if (!init(filename))
    {
      return 1;
    }

  process();
}

// Local Variables:
// mode: C++
// End:
